package com.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.server.common.base.SessionData;
import com.server.service.ICommonDataService;
import com.server.service.IGradeManagementService;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("grade")
public class GradeManagementController {

	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private IGradeManagementService gradeManagementService;
	
	 /**
	 * 添加评分
	 * @param request
	 * @param response
	 * @param type
	 * @author ServerZhang
	 * @date 2016年8月10日
	 */
	@RequestMapping("add/{type}")
	public void add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String type){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		if (StringUtils.isEmpty(userName)) {
			OutputUtil.returnSuccess(response, false);
			return;
		}
		/*String typeId = request.getParameter("typeId");
		String accept = request.getParameter("accept");
		String acceptCon = request.getParameter("acceptCon");
		String quality = request.getParameter("quality");
		String qualityCon = request.getParameter("qualityCon");*/
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("typeId", request.getParameter("typeId"));//类型关联id
		inputMap.put("type", type);//类型  关联表名
		inputMap.put("accept", request.getParameter("accept"));//可接受程度评分
		inputMap.put("acceptCon", request.getParameter("acceptCon"));//接受程度评价
		inputMap.put("quality", request.getParameter("quality"));//质量评分
		inputMap.put("qualityCon", request.getParameter("qualityCon"));//质量评价
		Boolean add = commonDataService.add(userName, "education.course_score", inputMap);
		OutputUtil.returnSuccess(response, add);
	}
	 /**
	 * 获取总评分和我的评分
	 * @param request
	 * @param response
	 * @param type
	 * @author ServerZhang
	 * @date 2016年8月10日
	 */
	@RequestMapping("score/{type}/{id}")
	public void score(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String type,
			@PathVariable Integer id){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		Map<String, Object> score = gradeManagementService.getScore(type, id, userName);
		OutputUtil.returnString(response, JSONSerielizer.toJson(score, null, null));
	}
}
